package com.example.system.controller;

import cn.hutool.core.util.RandomUtil;
import com.example.starter.util.Response;
import com.example.system.constant.SMS;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 王令
 * @since 2022/12/10
 */
@Slf4j
@RequestMapping("/sms")
@RestController
@RequiredArgsConstructor
public class SmsController {

    private final RedisTemplate<String, Object> redisTemplate;

    @PostMapping("/code/{phone}")
    public Response<Boolean> sendCode(@PathVariable("phone") String phone) {
        final String code = RandomUtil.randomNumbers(6);
        log.info("phone: {}, code: {}", phone, code);
        redisTemplate.opsForValue().set(SMS.REGISTER_CODE + phone, code);
        return Response.ok();
    }
}
